require("../../../dist/toast/toast").default;

var t = getApp();

Component({
    addGlobalClass: !0,
    properties: {
        show: {
            value: !1,
            type: Boolean
        },
        dispatch: {
            value: "",
            type: Number
        },
        need_lat: {
            value: "",
            type: Number
        },
        status: {
            value: "",
            type: Number
        },
        polygons: {
            value: [],
            type: Array
        },
        goodsStyle: {
            value: !0,
            type: Boolean
        },
        confirmBtn: {
            value: !0,
            type: Boolean
        }
    },
    data: {
        addressData: [],
        form: {},
        create: !0,
        popAddAddress: !1,
        addressName: {},
        districtVal: "",
        strShow: !1,
        provinceData: [],
        pickerValue: [ 0, 0, 0 ],
        delivery_status: 0,
        selected: "",
        is_point: "",
        mapshow: !1,
        tapEditOrAdd: 1,
        isclick: 0,
        add_address: !0,
        country_code: 0,
        addressCodeHospital: !1,
        HospitalForm: {}
    },
    lifetimes: {
        attached: function() {
            console.log("在组件实例进入页面节点树时执行"), this.triggerEvent("childShowRead");
        }
    },
    pageLifetimes: {
        show: function() {
            console.log("加载地址数据初始化"), this._initAddressInfo(), this.getIsOpenAddress();
            var t = wx.getStorageSync("yz_basic_info");
            this.setData({
                address_parse_open: t.plugin_setting.address_parse_open ? t.plugin_setting.address_parse_open : 0,
                country_code: !!t.home.system.country_code && t.home.system.country_code
            }), console.log("xpc aa", this.data.country_code);
        }
    },
    observers: {
        show: function(t) {
            1 == t && this.getAddress();
        },
        dispatch: function(t) {
            this.setData({
                delivery_status: t
            });
        },
        status: function(t) {
            this.setData({
                selected: t
            });
        }
    },
    methods: {
        tapSelect: function(t) {
            var e = t.currentTarget.dataset.item, a = this.data.level - 1;
            4 == this.data.level ? (this.data.HospitalForm[this.data.level] = {}, this.data.HospitalForm[this.data.level].id = e.id, 
            this.data.model.id = e.id, this.data.model.plugin_data[a].value = e.room_no, this.data.model.plugin_data[this.data.level].value = e.department) : (this.data.model.plugin_data[a].value = e.name, 
            this.data.HospitalForm[this.data.level] = {}, this.data.HospitalForm[this.data.level].id = e.id), 
            this.setData({
                HospitalForm: this.data.HospitalForm,
                model: this.data.model
            });
        },
        confirmTapBtn: function() {
            var e = this;
            if (!this.data.model.id) return t.tips("请选择详细地址");
            var a = {
                address_id: this.data.model.id,
                user_name: this.data.form.username,
                mobile: this.data.form.mobile,
                is_default: this.data.model.isdefault
            }, s = t.getNetAddresss("plugin.address-code.frontend.index.set-hospital");
            t._getNetWork({
                url: s,
                data: a,
                success: function(a) {
                    var s = a.data;
                    1 == s.result ? (console.log(s, "resdata"), e.triggerEvent("closeChild")) : t.tips(s.msg);
                },
                fail: function(t) {
                    reject(t);
                }
            });
        },
        onClickRight: function() {
            this.setData({
                addressCodeHospital: !1,
                addressCodeInfo: []
            });
        },
        tapSearch: function(e) {
            var a = this, s = e.currentTarget.dataset.ind + 1;
            this.setData({
                level: s
            });
            var i = {
                level: s
            };
            if (s > 1) {
                if (!this.data.HospitalForm[s - 1]) return t.tips("请先选择".concat(this.data.model.plugin_data[e.currentTarget.dataset.ind - 1].name));
                i.pid = this.data.HospitalForm[s - 1].id;
            }
            var d = t.getNetAddresss("plugin.address-code.frontend.index.search-hospital");
            t._getNetWork({
                url: d,
                data: i,
                success: function(t) {
                    var e = t.data;
                    1 == e.result && (4 != s || 4 == s) && a.setData({
                        addressCodeInfo: e.data.list,
                        addressCodeHospital: !0
                    });
                },
                fail: function(t) {
                    reject(t);
                }
            }), console.log(s);
        },
        tapToParse: function() {
            this.setData({
                popAddAddress: !1,
                address_parse_show: !0
            });
        },
        address_parseClose: function(t) {
            this.setData({
                address_parse_show: !1,
                popAddAddress: !0
            });
        },
        toPostMethod: function(t) {
            var e = t.detail;
            this.setData({
                "form.address": e.address,
                "form.province": e.province,
                "form.city": e.city,
                "form.district": e.region,
                "form.username": e.name,
                "form.mobile": e.mobile,
                "form.country_code": e.country_code,
                addressName: "".concat(e.province).concat(e.city).concat(e.region)
            }), this._getStreet(e.region_id, e.street), this.address_parseClose();
        },
        getDefaultAddress: function() {
            return new Promise(function(e, a) {
                var s = t.getNetAddresss("plugin.address-code.frontend.index.get-user-info");
                t._getNetWork({
                    url: s,
                    success: function(t) {
                        var a = t.data;
                        if (1 == a.result) {
                            var s = a.data;
                            s.id = 0, e(s);
                        }
                    },
                    fail: function(t) {
                        a(t);
                    }
                });
            });
        },
        getAddress: function() {
            this.addressData = [];
            var e = this, a = t.getNetAddresss("member.member-address.index");
            t._getNetWork({
                url: a,
                success: function(t) {
                    var a = t.data;
                    if (1 == a.result) {
                        var s = a.data;
                        s = a.data.filter(function(t) {
                            return 1 === t.isdefault;
                        }), console.log(a.data, "ddddddddd"), e.setData({
                            popupSpecs: !0,
                            addressData: a.data,
                            defaultAddress: s
                        });
                    } else wx.showToast({
                        icon: "none",
                        title: a.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        },
        showLocation: function() {
            this.setData({
                mapshow: !0
            });
        },
        goLocation_ro: function() {
            var e = this;
            t.getGeocoder(this.data.addressName + this.data.form.address).then(function(a) {
                a = t.qqMapTransBMap(parseFloat(a.lng), parseFloat(a.lat)), e.setData({
                    is_point: a
                });
            }, function(t) {
                e.showLocation();
            }).catch(function(t) {
                e.showLocation();
            });
        },
        selectAddress: function(t) {
            var e = t.currentTarget.dataset.addr;
            this.triggerEvent("confirm", e);
        },
        confirmSelect: function(e) {
            if ("noshow" == e.detail) this.setData({
                mapshow: !1
            }); else {
                var a = e.detail;
                a = t.qqMapTransBMap(parseFloat(a.lng), parseFloat(a.lat)), this.setData({
                    is_point: a,
                    mapshow: !1
                });
            }
        },
        code_editAddress: function(t) {
            var e = t;
            console.log(t, "dddddddddddd1"), this.setData({
                "form.address_id": e.id,
                "form.username": e.username || e.user_name,
                "form.mobile": e.mobile,
                "form.province": e.province,
                "form.city": e.city,
                "form.district": e.district,
                "form.address": e.address,
                "form.street": e.street,
                "form.country_code": e.country_code,
                "form.isDefault": 0 !== e.isdefault,
                "is_point.lng": e.longitude,
                "is_point.lat": e.latitude,
                tapEditOrAdd: 1,
                model: e
            }), console.log(this.data.form, "dddddddddddd1"), this.setData({
                create: !1,
                codeEdit: !0
            }), e.district_id && this._getStreet(e.district_id), this.setData({
                addressName: this.data.form.province + " " + this.data.form.city + " " + this.data.form.district
            });
        },
        editAddress: function(t) {
            var e = t.currentTarget.dataset.item;
            console.log("xpc", e), this.setData({
                "form.address_id": e.id,
                "form.username": e.username,
                "form.mobile": e.mobile,
                "form.province": e.province,
                "form.city": e.city,
                "form.district": e.district,
                "form.address": e.address,
                "form.street": e.street,
                "form.country_code": e.country_code,
                "form.isDefault": 0 !== e.isdefault,
                "is_point.lng": e.longitude,
                "is_point.lat": e.latitude,
                tapEditOrAdd: 1,
                model: e
            }), this.setData({
                create: !1,
                popAddAddress: !0
            }), this._getStreet(e.district_id), this.setData({
                addressName: this.data.form.province + " " + this.data.form.city + " " + this.data.form.district
            });
        },
        popAddressClose: function() {
            this.setData({
                popAddAddress: !1
            });
        },
        countryCodeInput: function(t) {
            var e = t.detail.value;
            this.setData({
                "form.country_code": e
            });
        },
        usernameinp: function(t) {
            var e = t.detail.value;
            this.setData({
                "form.username": e
            });
        },
        mobileinp: function(t) {
            var e = t.detail.value;
            this.setData({
                "form.mobile": e
            });
        },
        isDefaultChange: function(t) {
            var e = t.detail;
            this.setData({
                "form.isDefault": e
            });
        },
        addressinp: function(t) {
            var e = t.detail.value;
            this.setData({
                "form.address": e
            });
        },
        _closeDateLw: function() {
            this.setData({
                openDateLw: !1
            });
        },
        _initAddressInfo: function() {
            var e = this;
            wx.getStorage({
                key: "provinceData",
                success: function(t) {
                    console.log("地址信息准备就绪"), e._getProvinceData();
                },
                fail: function(a) {
                    console.log("省fail"), t._getNetWork({
                        url: t.getNetAddresss("member.member-address.address"),
                        success: function(t) {
                            var a = t.data, s = a.data.province, i = a.data.city, d = a.data.district;
                            wx.setStorage({
                                key: "provinceData",
                                data: s
                            }), wx.setStorage({
                                key: "cityData",
                                data: i
                            }), wx.setStorage({
                                key: "districtData",
                                data: d
                            }), e._initAddressInfo();
                        },
                        fail: function(t) {},
                        state: !0
                    });
                }
            });
        },
        _getProvinceData: function() {
            var t = this;
            wx.getStorage({
                key: "provinceData",
                success: function(e) {
                    t.setData({
                        provinceData: e.data
                    }), t.setData({
                        "selectAddressData[0].name": e.data[t.data.pickerValue[0]].areaname,
                        "selectAddressData[0].id": e.data[t.data.pickerValue[0]].id,
                        "selectAddressData[0].index": t.data.pickerValue[0]
                    }), t._getCityData();
                }
            });
        },
        _getCityData: function() {
            var t = this.data.selectAddressData[0], e = this;
            wx.getStorage({
                key: "cityData",
                success: function(a) {
                    for (var s = [], i = 0; i < a.data.length; i++) a.data[i].parentid == t.id && s.push(a.data[i]);
                    e.setData({
                        cityData: s
                    }), e.setData({
                        "selectAddressData[1].name": s[e.data.pickerValue[1]].areaname,
                        "selectAddressData[1].id": s[e.data.pickerValue[1]].id,
                        "selectAddressData[1].index": e.data.pickerValue[1]
                    }), e._getDistrictData();
                }
            });
        },
        _getDistrictData: function() {
            var t = this.data.selectAddressData[1], e = this;
            wx.getStorage({
                key: "districtData",
                success: function(a) {
                    console.log("地区读取成功");
                    for (var s = [], i = 0; i < a.data.length; i++) a.data[i].parentid == t.id && s.push(a.data[i]);
                    e.setData({
                        districtData: s
                    }), e.setData({
                        "selectAddressData[2].name": s[e.data.pickerValue[2]].areaname,
                        "selectAddressData[2].id": s[e.data.pickerValue[2]].id,
                        "selectAddressData[2].index": e.data.pickerValue[2]
                    });
                }
            });
        },
        openDateLwtbtn: function() {
            this.setData({
                openDateLw: !0
            }), this.data.goodsStyle || this._initAddressInfo(), console.log("aaaaaaaaaaaaaaa");
        },
        bindChange: function(t) {
            var e = t.detail.value;
            this.data.pickerValue[0] != e[0] && (e[1] = 0, e[2] = 0), this.data.pickerValue[1] != e[1] && (e[2] = 0), 
            this.setData({
                "pickerValue[0]": e[0],
                "pickerValue[1]": e[1],
                "pickerValue[2]": e[2]
            }), this._initAddressInfo();
        },
        _okAddress: function() {
            var t = this.data.selectAddressData;
            this.setData({
                "form.street": "",
                districtVal: "",
                addressName: t[0].name + " " + t[1].name + " " + t[2].name,
                "form.province": t[0].name,
                "form.city": t[1].name,
                "form.district": t[2].name
            }), this._getStreet(t[2].id), this._closeDateLw();
        },
        _getStreet: function(e) {
            var a = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "", s = this, i = t.getNetAddresss("member.memberAddress.street");
            i += "&district_id=" + e, t._getNetWork({
                url: i,
                success: function(e) {
                    var i = e.data;
                    1 != i.result || t._isTextEmpty(i.data) ? s.setData({
                        strShow: !1
                    }) : !t._isTextEmpty(i.data) && i.data.length > 0 ? (s.setData({
                        districtVal: i.data,
                        strShow: !0
                    }), a && s.setData({
                        "form.street": a
                    })) : s.setData({
                        strShow: !1
                    });
                },
                fail: function(t) {
                    s.setData({
                        strShow: !1
                    }), console.log(t);
                }
            });
        },
        streetChoose: function() {
            1 == this.data.is_region && t._isTextEmpty(this.data.addressName) ? wx.showToast({
                icon: "none",
                title: "请先选择所在地区",
                duration: 1500
            }) : this.setData({
                streetShow: !0,
                opren: !1
            });
        },
        streetClose: function() {
            this.setData({
                streetShow: !1,
                opren: !0
            });
        },
        streetConfirm: function(t) {
            var e = t.target.dataset.strname;
            this.setData({
                "form.street": e,
                streetShow: !1,
                opren: !0
            });
        },
        appendAddress: function(e) {
            var a = this, s = this, i = e.currentTarget.dataset.apptype;
            if (t._isTextEmpty(this.data.form.country_code) && this.data.country_code) wx.showToast({
                icon: "none",
                title: "请输入区号",
                duration: 1500
            }); else if (t._isTextEmpty(this.data.form.username)) wx.showToast({
                icon: "none",
                title: "请输入收货人姓名",
                duration: 1500
            }); else if (t._isTextEmpty(this.data.form.mobile)) wx.showToast({
                icon: "none",
                title: "请输入联系电话",
                duration: 1500
            }); else if (/^[0-9]{1,}$/.test(this.data.form.mobile)) {
                if (1 == this.data.is_region && t._isTextEmpty(this.data.addressName)) wx.showToast({
                    icon: "none",
                    title: "请选择所在区域",
                    duration: 1500
                }); else if (t._isTextEmpty(this.data.form.address)) wx.showToast({
                    icon: "none",
                    title: "请输入详细地址",
                    duration: 1500
                }); else if (1 != this.data.isclick) {
                    this.data.isclick = 1;
                    var d = {};
                    d = {
                        username: this.data.form.username,
                        mobile: this.data.form.mobile,
                        province: this.data.form.province,
                        city: this.data.form.city,
                        district: this.data.form.district,
                        address: this.data.form.address,
                        isdefault: this.data.form.isDefault ? 1 : 0,
                        address_id: this.data.form.address_id
                    }, this.data.is_point && (d.longitude = this.data.is_point.lng, d.latitude = this.data.is_point.lat), 
                    this.data.strShow && (d.street = this.data.form.street), this.data.country_code && (d.country_code = this.data.form.country_code);
                    var o = "";
                    o = "update" === i ? t.getNetAddresss("member.member-address.update") : t.getNetAddresss("member.member-address.store"), 
                    t._postNetWork({
                        url: o,
                        showToastIn: !1,
                        data: d,
                        success: function(t) {
                            var e = t.data;
                            setTimeout(function() {
                                a.data.isclick = 0;
                            }, 1e3), 1 == e.result ? a.data.codeEdit ? s.triggerEvent("confirm", e.data) : (wx.showToast({
                                icon: "none",
                                title: e.msg,
                                duration: 1500
                            }), s.triggerEvent("confirm", e.data), s.popAddressClose(), s.setData({
                                "form.username": "",
                                "form.mobile": "",
                                "form.province": "",
                                "form.city": "",
                                "form.district": "",
                                "form.street": "",
                                "form.address": "",
                                "form.isDefault": !1,
                                "form.country_code": "86",
                                addressName: ""
                            })) : wx.showToast({
                                icon: "none",
                                title: e.msg,
                                duration: 1500
                            });
                        },
                        fail: function(t) {}
                    });
                }
            } else wx.showToast({
                icon: "none",
                title: "请输入联系电话",
                duration: 1500
            });
        },
        onCloseAdd: function() {
            this.setData({
                show: !1
            });
        },
        addAddress: function() {
            this.setData({
                is_point: "",
                tapEditOrAdd: 2
            }), this.showAddAddress();
        },
        getIsOpenAddress: function() {
            var e = this, a = t.getNetAddresss("member.member-address.is-region");
            t._getNetWork({
                url: a,
                success: function(a) {
                    var s = a.data;
                    1 != s.result || t._isTextEmpty(s.data) ? console.log(s.msg) : e.setData({
                        is_region: s.data.is_region,
                        region_hide: s.data.region_hide
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        },
        showAddAddress: function() {
            var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : "";
            this.setData({
                "form.username": "",
                "form.mobile": "",
                "form.province": "",
                "form.city": "",
                "form.district": "",
                "form.street": "",
                "form.address": "",
                "form.isDefault": !1,
                "form.country_code": "86",
                addressName: "",
                popAddAddress: !0,
                create: !0,
                model: {}
            }), !t && this.data.defaultAddress.length <= 0 && this.setData({
                "form.isDefault": !0
            }), 2 != this.data.tapEditOrAdd || this.data.goodsStyle || this.setData({
                "form.isDefault": !0
            });
        }
    }
});